#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QMdiSubWindow>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    Init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

/******************* function ************************/
void MainWindow::Init(){
    this->codeName =  "UTF-8";
    this->setWindowTitle("editor");

    // 文件的
    connect(ui->actionNew, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByNew(bool)));
    connect(ui->actionOpen, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByOpen(bool)));
    connect(ui->actionUTF_8, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByUTF_8(bool)));
    connect(ui->actionGB2312, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByGB2312(bool)));
    connect(ui->actionSave, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredBySave(bool)));
    connect(ui->actionSaveAs, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredBySaveAs(bool)));
    connect(ui->actionExit, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByExit(bool)));

    // 编辑
    connect(ui->actionRedo, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByRedo(bool)));
    connect(ui->actionUndo, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByUndo(bool)));
    connect(ui->actionCut, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByCut(bool)));
    connect(ui->actionCopy, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByCopy(bool)));
    connect(ui->actionPaste, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByPaste(bool)));
    connect(ui->actionFind, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByFind(bool)));
    connect(ui->actionReplace, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByReplace(bool)));


    // 窗口
    connect(ui->actionClose, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByClose(bool)));
    connect(ui->actionCloseAll, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByCloseAll(bool)));
    connect(ui->actionTile, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByTile(bool)));
    connect(ui->actionCascade, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByCascade(bool)));

    // 语言
    connect(ui->actionCpp, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByCpp(bool)));
    connect(ui->actionJava, SIGNAL(triggered(bool)),  this, SLOT(doProcessTriggeredByJava(bool)));
    connect(ui->actionPython, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByPython(bool)));
    connect(ui->actionHtml, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByHtml(bool)));

    // 帮助
    connect(ui->actionAbout, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByAbout(bool)));
    connect(ui->actionAboutQt, SIGNAL(triggered(bool)), this, SLOT(doProcessTriggeredByAboutQt(bool)));

}
subWidget* MainWindow::getActiveChildForm(){
    subWidget *sub = NULL;
    QMdiSubWindow *subWin = ui->mdiArea->activeSubWindow();
    if(subWin == NULL) return sub;
    QWidget *wid = subWin->widget();
    if(wid == NULL) return sub;
    sub = (subWidget *)wid;
    return sub;
}
/******************* end func ************************/


/******************* slots ***************************/
// 文件
void MainWindow::doProcessTriggeredByNew(bool){
    subWidget *sub = new subWidget();
    ui->mdiArea->addSubWindow(sub);
    sub->setCodeName(codeName);
    sub->NewFile();
    sub->show();
}
void MainWindow::doProcessTriggeredByOpen(bool){
    subWidget *sub = new subWidget();
    ui->mdiArea->addSubWindow(sub);
    sub->setCodeName(codeName);
    sub->OpenFile();
    sub->show();
}
void MainWindow::doProcessTriggeredByUTF_8(bool){
    this->codeName = "UTF-8";
}
void MainWindow::doProcessTriggeredByGB2312(bool){
    this->codeName = "GB18030";
}
void MainWindow::doProcessTriggeredBySave(bool){
    QMdiSubWindow *subWin = ui->mdiArea->activeSubWindow();
    if(subWin == NULL) return;
    QWidget *wid = subWin->widget();
    subWidget *sub = (subWidget *)wid;
    sub->SaveFile();
}
void MainWindow::doProcessTriggeredBySaveAs(bool){
    QMdiSubWindow *subWin = ui->mdiArea->activeSubWindow();
    if(subWin == NULL) return;
    QWidget *wid = subWin->widget();
    subWidget *sub = (subWidget *)wid;
    sub->SaveAsFile();
}
void MainWindow::doProcessTriggeredByExit(bool){
    this->close();
}
// 编辑
void MainWindow::doProcessTriggeredByRedo(bool){
    subWidget *sub = getActiveChildForm();
    if(sub != NULL&&sub->geteditor()->isRedoAvailable())
        sub->geteditor()->redo();
}
void MainWindow::doProcessTriggeredByUndo(bool){
    subWidget *sub = getActiveChildForm();
    if(sub != NULL) sub->geteditor()->undo();
}
void MainWindow::doProcessTriggeredByCut(bool){
    subWidget *sub = getActiveChildForm();
    if(sub != NULL) sub->geteditor()->cut();
}
void MainWindow::doProcessTriggeredByCopy(bool){
    subWidget *sub = getActiveChildForm();
    if(sub != NULL) sub->geteditor()->copy();
}
void MainWindow::doProcessTriggeredByPaste(bool){
    subWidget *sub = getActiveChildForm();
    if(sub != NULL) sub->geteditor()->paste();
}

void MainWindow::doProcessTriggeredByFind(bool)
{
    subWidget *sub = getActiveChildForm();
    if(sub != NULL) sub->findDlg->show();
}
void MainWindow::doProcessTriggeredByReplace(bool)
{
    subWidget *sub = getActiveChildForm();
    if(sub != NULL) sub->replaceDlg->show();
}

// 窗体
void MainWindow::doProcessTriggeredByClose(bool){
    ui->mdiArea->closeActiveSubWindow();
}

void MainWindow::doProcessTriggeredByCloseAll(bool){
    ui->mdiArea->closeAllSubWindows();
}

void MainWindow::doProcessTriggeredByTile(bool){
    ui->mdiArea->tileSubWindows();
}

void MainWindow::doProcessTriggeredByCascade(bool){
    ui->mdiArea->cascadeSubWindows();
}

// 语言
void MainWindow::doProcessTriggeredByCpp(bool){
    subWidget *sub = getActiveChildForm();
    if(sub != NULL) sub->setLexer(".cpp");
}

void MainWindow::doProcessTriggeredByJava(bool){
    subWidget *sub = getActiveChildForm();
    if(sub != NULL) sub->setLexer(".java");
}

void MainWindow::doProcessTriggeredByPython(bool){
    subWidget *sub = getActiveChildForm();
    if(sub != NULL) sub->setLexer(".py");
}

void MainWindow::doProcessTriggeredByHtml(bool){
    subWidget *sub = getActiveChildForm();
    if(sub != NULL) sub->setLexer(".html");
}

// 帮助
void MainWindow::doProcessTriggeredByAbout(bool){
//    QMessageBox::about(this, "关于", "作者：宇宙无敌最可爱的短笛大魔王    ");
    QMessageBox message(QMessageBox::NoIcon,"作者",
                        "<font font size='18' color='#66ccff' style='font-family:楷体'>就是宇宙无敌最可爱的短笛大魔王啊(*^o^*)</font>",
                        QMessageBox::Ok, NULL);

    message.exec();
}
void MainWindow::doProcessTriggeredByAboutQt(bool){
    QMessageBox::about(this, "关于Qt", "Qt Creator 4.11.1\n"
                                     "Based on Qt 5.14.1 (MSVC 2017, 32 bit)\n"
                                     "Copyright 2008-2019 The Qt Company Ltd. All rights reserved.\n"
                                     "The program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\n");
}
/******************* end slots ***********************/

